package com.guoq.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guoq.mapper.ShopMapper;
import com.guoq.pojo.EUDataGridResult;
import com.guoq.pojo.Result;
import com.guoq.pojo.Shop;
import com.guoq.pojo.ShopExample;
import com.guoq.pojo.ShopExample.Criteria;
import com.guoq.service.IShopService;
import com.guoq.utils.IDUtils;
import com.guoq.utils.Strkit;

@Service("shopService")
public class ShopServiceImpl implements IShopService {

	@Resource
	private ShopMapper shopMapper;

	@Override
	public List<Shop> getByUserId(String userid) {
		// TODO Auto-generated method stub
		ShopExample example = new ShopExample();
		Criteria criteria = example.createCriteria();
		criteria.andStatusEqualTo((byte)0);
		criteria.andUserIdEqualTo(userid);
		List<Shop> list = shopMapper.selectByExample(example);
		return list;
	}

	@Override
	public List<Shop> getList() {
		// TODO Auto-generated method stub
		ShopExample example = new ShopExample();
		Criteria criteria = example.createCriteria();
		criteria.andStatusEqualTo((byte)0);
		example.setOrderByClause("id");
		List<Shop> list = shopMapper.selectByExample(example);
		return list;
	}
	
	@Override
	public EUDataGridResult getPageList(Shop record, Integer page, Integer rows) {
		
		// TODO Auto-generated method stub
		ShopExample example = new ShopExample();
		Criteria criteria = example.createCriteria();
		
		String name = record.getName();
		if(Strkit.notBlank(name)){
			criteria.andNameLike(name);
		}
		
		example.setOrderByClause("createtime desc");
		
		//分页处理
		PageHelper.startPage(page, rows);
		List<Shop> list = shopMapper.selectByExample(example);
		//创建一个返回值对象
		EUDataGridResult result = new EUDataGridResult();
		result.setRows(list);
		//取记录总条数
		PageInfo<Shop> pageInfo = new PageInfo<>(list);
		result.setTotal(pageInfo.getTotal());
		return result;
	}

	@Override
	public List<Shop> selectByExample(ShopExample example) {
		// TODO Auto-generated method stub
		List<Shop> list = shopMapper.selectByExample(example);
		return list;
	}

	@Override
	public Shop selectByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		Shop shop = shopMapper.selectByPrimaryKey(id);
		return shop;
	}

	@Override
	public int countByExample(ShopExample example) {
		// TODO Auto-generated method stub
		int count = shopMapper.countByExample(example);
		return count;
	}

	@Override
	public Result create(Shop bean) throws Exception {

		String shopid = IDUtils.genShopId();	//自动生成店铺id
		
		Result result = this.insert(shopid, bean);
		if(result.getStatus() != 200){
			throw new Exception();
		}
		return Result.ok();
	}
	
	/**
	 * 添加商品描述
	 *<P>开发者:郭强
	 *<P>方法名: insertItemDesc
	 *<P>描述: 
	 *@param itemId
	 *@param desc
	 *@return   
	 *@exception   
	 *@since  1.0.0
	 */
	private Result insert(String shopId, Shop shop){
		
		shop.setShopId(shopId);
		shop.setStatus((byte)0);
		shop.setCreatetime(new Date());
		shop.setUpdatetime(new Date());
		shopMapper.insertSelective(shop);
		return Result.ok();
	}

	/**
	 * 删除
	 */
	@Override
	public Result delete(Shop record) throws Exception {

		Result result = null;
		
		record.setStatus((byte)2);
		record.setUpdatetime(new Date());
		int num = shopMapper.updateByPrimaryKeySelective(record);
		
		if(num == 1){
			result = Result.ok();
		}else {
			result = Result.build(500,"error");
			throw new Exception();
		}
		
		return result;
	}

	/**
	 * 编辑
	 */
	@Override
	public Result update(Shop bean) throws Exception {

		Result result = null;
		int num = shopMapper.updateByPrimaryKeySelective(bean);
		
		if(num == 1){
			result = Result.ok();
		}else {
			result = Result.build(500,"error");
			throw new Exception();
		}
		
		return result;
	}
}
